
package com.kanjian.music.adapter;

import java.util.ArrayList;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.LiveActivity;
import com.kanjian.music.activity.NextLiveActivity;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Live;
import com.kanjian.music.view.CircleImageView;
import com.kanjian.music.volley.VolleyQueue;

public class LiveListAdapter extends BaseAdapter {

    private ArrayList<Live> mDataList;

    private BaseActivity mContext;

    private ListView mListView;

    private LayoutInflater mInflater;

    public LiveListAdapter(BaseActivity context, ListView listView) {
        this.mContext = context;
        this.mListView = listView;
        this.mInflater = LayoutInflater.from(mContext);
        registerOnItemClickListener();
    }

    private void registerOnItemClickListener() {
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Live live = getItem(position);
                if (live.mIsPlay == 0) {
                    // 正在直播
                    Intent intent = new Intent();
                    intent.setClass(mContext, LiveActivity.class);
                    mContext.startActivityWithAnimation(intent);
                } else {
                    Intent intent = new Intent();
                    intent.putExtra(IntentConstants.LIVEID, live.mLiveId);
                    intent.setClass(mContext, NextLiveActivity.class);
                    mContext.startActivityWithAnimation(intent);
                }
            }
        });
    }

    public void setLiveList(ArrayList<Live> list) {
        if (mDataList != null) {
            mDataList.clear();
            mDataList.addAll(list);
        } else {
            mDataList = list;
        }
    }

    @Override
    public int getCount() {
        return mDataList != null ? mDataList.size() : 0;
    }

    @Override
    public Live getItem(int position) {
        return mDataList != null ? mDataList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = mInflater.inflate(R.layout.item_list_live, null);
            holder = new ViewHolder();
            holder.mIv_activityPic = (NetworkImageView) convertView
                    .findViewById(R.id.item_list_live_activity_pic);
            holder.mIv_userPic = (CircleImageView) convertView
                    .findViewById(R.id.item_list_live_user_pic);
            holder.mTv_activityName = (TextView) convertView
                    .findViewById(R.id.item_list_live_activity_name);
            holder.mTv_activityTime = (TextView) convertView
                    .findViewById(R.id.item_list_live_activity_time);
            holder.mTv_liveStatus = (TextView) convertView.findViewById(R.id.item_list_live_status);
            holder.mTv_userName = (TextView) convertView
                    .findViewById(R.id.item_list_live_user_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Live live = getItem(position);
        holder.mIv_activityPic.setImageUrl(live.mActivityPic, VolleyQueue.getImageLoader());
        holder.mIv_userPic.setImageUrl(live.mUserPic, VolleyQueue.getImageLoader());
        holder.mTv_activityName.setText(live.mActivityName);
        holder.mTv_activityTime.setText(live.getDisplayTimeStr());
        holder.mTv_liveStatus.setText(live.mIsPlay == 0 ? "正在直播" : "即将开播");
        holder.mTv_userName.setText(live.mUserName);
        return convertView;
    }

    static class ViewHolder {
        NetworkImageView mIv_activityPic;

        TextView mTv_liveStatus;

        TextView mTv_userName;

        TextView mTv_activityName;

        TextView mTv_activityTime;

        CircleImageView mIv_userPic;
    }

}
